package top.whysu.rongcloud.api;

import io.rong.RongCloud;
import io.rong.methods.chatroom.ban.Ban;
import io.rong.methods.chatroom.block.Block;
import io.rong.methods.chatroom.demotion.Demotion;
import io.rong.methods.chatroom.distribute.Distribute;
import io.rong.methods.chatroom.gag.Gag;
import io.rong.methods.chatroom.keepalive.Keepalive;
import io.rong.methods.chatroom.whitelist.Whitelist;
import io.rong.models.response.ResponseResult;
import io.rong.util.GsonUtil;
import io.rong.util.HttpUtil;
import top.whysu.rongcloud.api.dto.ChatroomMembers;

import java.net.HttpURLConnection;
import java.net.URLEncoder;

public class Chatrooms {
    private static final String UTF8 = "UTF-8";
    private static final String PATH = "chatroom";
    private String appKey;
    private String appSecret;
    public Block block;
    public Gag gag;
    public Ban ban;
    public Keepalive keepalive;
    public Demotion demotion;
    public Whitelist whiteList;
    public Distribute distribute;
    private RongCloud rongCloud;
    private RongCloudSMS rongCloudSMS;

    public RongCloud getRongCloud() {
        return this.rongCloud;
    }

    public RongCloudSMS getRongCloudSMS() {
        return rongCloudSMS;
    }

    public void setRongCloudSMS(RongCloudSMS rongCloudSMS) {
        this.rongCloudSMS = rongCloudSMS;
    }

    public void setRongCloud(RongCloudSMS rongCloudSMS) {
        this.rongCloudSMS=rongCloudSMS;

    }

    public Chatrooms(String appKey, String appSecret) {
        this.appKey = appKey;
        this.appSecret = appSecret;
        this.gag = new Gag(appKey, appSecret);
        this.keepalive = new Keepalive(appKey, appSecret);
        this.demotion = new Demotion(appKey, appSecret);
        this.whiteList = new Whitelist(appKey, appSecret);
        this.block = new Block(appKey, appSecret);
        this.distribute = new Distribute(appKey, appSecret);
        this.ban = new Ban(appKey, appSecret);
    }

    public ChatRoomsResult get(ChatroomMembers chatroomMembers) throws Exception {
            StringBuilder sb = new StringBuilder();
            sb.append("&chatroomId=").append(URLEncoder.encode(chatroomMembers.getChatroomId(), "UTF-8"));
            String body = sb.toString();
            if (body.indexOf("&") == 0) {
                body = body.substring(1, body.length());
            }
            HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/query.json", "application/x-www-form-urlencoded");
            HttpUtil.setBodyParameter(body, conn);
            return (ChatRoomsResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ChatRoomsResult.class);
    }
    public CheckChatRoomUsersResult isExists(ChatroomMembers member) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(member.getChatroomId(), "UTF-8"));

        for(int i = 0; i < member.getUserId().length; ++i) {
            String userId  = member.getUserId()[i];
            sb.append("&userId=").append(URLEncoder.encode(userId, "UTF-8"));
        }
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/users/exist.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (CheckChatRoomUsersResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), CheckChatRoomUsersResult.class);
    }

    public ResponseResult gag_add(ChatroomMembers member) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(member.getChatroomId(), "UTF-8"));
        sb.append("&minute=").append(URLEncoder.encode(member.getMinute(), "UTF-8"));

        for(int i = 0; i < member.getUserId().length; ++i) {
            String userId  = member.getUserId()[i];
            sb.append("&userId=").append(URLEncoder.encode(userId, "UTF-8"));
        }
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/gag/add.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);
    }


    public ResponseResult gag_rollback(ChatroomMembers member) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(member.getChatroomId(), "UTF-8"));

        for(int i = 0; i < member.getUserId().length; ++i) {
            String userId  = member.getUserId()[i];
            sb.append("&userId=").append(URLEncoder.encode(userId, "UTF-8"));
        }
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/gag/rollback.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);
    }

    public ChatRoomBagResult gag_list(String chatroomId) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(chatroomId, "UTF-8"));
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/gag/list.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ChatRoomBagResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ChatRoomBagResult.class);
    }

    public ResponseResult block_add(ChatroomMembers member) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(member.getChatroomId(), "UTF-8"));
        sb.append("&minute=").append(URLEncoder.encode(member.getMinute(), "UTF-8"));
        for(int i = 0; i < member.getUserId().length; ++i) {
            String userId  = member.getUserId()[i];
            sb.append("&userId=").append(URLEncoder.encode(userId, "UTF-8"));
        }
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/block/add.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);
    }

    public ResponseResult block_rollback(ChatroomMembers member) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(member.getChatroomId(), "UTF-8"));
        for(int i = 0; i < member.getUserId().length; ++i) {
            String userId  = member.getUserId()[i];
            sb.append("&userId=").append(URLEncoder.encode(userId, "UTF-8"));
        }
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/block/rollback.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);
    }

    public ChatRoomBagResult ban_query() throws Exception {

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/ban/query.json", "application/x-www-form-urlencoded");

        return (ChatRoomBagResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ChatRoomBagResult.class);
    }

    public ResponseResult ban_add(ChatroomMembers member) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&minute=").append(URLEncoder.encode(member.getMinute(), "UTF-8"));
        for(int i = 0; i < member.getUserId().length; ++i) {
            String userId  = member.getUserId()[i];
            sb.append("&userId=").append(URLEncoder.encode(userId, "UTF-8"));
        }
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/ban/add.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);
    }

    public ResponseResult ban_remove(ChatroomMembers member) throws Exception {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < member.getUserId().length; ++i) {
            String userId  = member.getUserId()[i];
            sb.append("&userId=").append(URLEncoder.encode(userId, "UTF-8"));
        }
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/ban/remove.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);
    }

    public ChatRoomBagResult block_list(String chatroomId) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(chatroomId, "UTF-8"));
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }
        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/block/list.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ChatRoomBagResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ChatRoomBagResult.class);
    }


    public ResponseResult whitelist_add(ChatroomMembers member) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(member.getChatroomId(), "UTF-8"));
        for(int i = 0; i < member.getUserId().length; ++i) {
            String userId  = member.getUserId()[i];
            sb.append("&userId=").append(URLEncoder.encode(userId, "UTF-8"));
        }
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/whitelist/add.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);
    }

    public ResponseResult whitelist_remove(ChatroomMembers member) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(member.getChatroomId(), "UTF-8"));
        for(int i = 0; i < member.getUserId().length; ++i) {
            String userId  = member.getUserId()[i];
            sb.append("&userId=").append(URLEncoder.encode(userId, "UTF-8"));
        }
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/whitelist/remove.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ResponseResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ResponseResult.class);
    }
    public ChatRoomWhitelistResult whitelist_query(String chatroomId) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("&chatroomId=").append(URLEncoder.encode(chatroomId, "UTF-8"));
        String body = sb.toString();
        if (body.indexOf("&") == 0) {
            body = body.substring(1, body.length());
        }
        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(this.rongCloudSMS.getApiHostType(), this.appKey, this.appSecret, "/chatroom/user/whitelist/query.json", "application/x-www-form-urlencoded");
        HttpUtil.setBodyParameter(body, conn);
        return (ChatRoomWhitelistResult)GsonUtil.fromJson(HttpUtil.returnResult(conn), ChatRoomWhitelistResult.class);
    }
}
